<template>
    <ul class="app-header-nav">
        <li class="home">
            <RouterLink to="/">首页</RouterLink>
        </li>
        <!-- 解决鼠标点击一级标签后还会出现二级标签的问题 -->
        <li v-for="item in navList" :key="item.id" @mousemove="show(item)" @mouseleave="hide(item)">
            <RouterLink :to="`/category/${item.id}`" @click="hide(item)">{{ item.name }}</RouterLink>
            <div class="layer" :class="{open:item.open}">
                <ul>
                    <li v-for="sub in item.children" :key="sub.id">
                        <RouterLink :to="`/category/sub/${sub.id}`" @click="hide(item)">
                            <img v-lazy="sub.picture" alt="">
                            <p>{{ sub.name }}</p>
                        </RouterLink>
                    </li>
                </ul>
            </div>
        </li>
    </ul>
</template>

<script setup>
import { useCategoryStore } from '@/store/category'
import { computed } from 'vue';

const userCategory = useCategoryStore()
const navList = computed(() => {
    return userCategory.topNavList
})

const show = (item) => {
    userCategory.show(item)
}
const hide = (item) => {
    userCategory.hide(item)
}
</script>
<style lang='scss' scoped>
.app-header-nav {
    width: 820px;
    display: flex;
    justify-content: space-around;
    padding-left: 40px;

    >li {
        position: relative;
        margin-right: 40px;
        width: 38px;
        text-align: center;

        >a {
            font-size: 16px;
            line-height: 32px;
            height: 32px;
            display: inline-block;
        }

        &:hover {
            >a {
                color: $mainColor;
                border-bottom: 1px solid $mainColor;
            }

            // >.layer {
            //     height: 132px;
            //     opacity: 1;
            // }
        }

        .layer {
            position: absolute;
            left: -200px;
            top: 56px;
            min-width: 200px;
            height: 0;
            overflow: hidden;
            opacity: 0;
            background-color: #fff;
            box-shadow: 0 0 5px #ccc;
            transition: all .2s .1s;
            z-index: 99;

            &.open {
                height: 132px;
                opacity: 1;
            }

            ul {
                display: flex;
                flex-wrap: no-wrap;
                padding: 0 70px;
                align-items: center;
                height: 132px;

                li {
                    width: 110px;
                    text-align: center;

                    img {
                        width: 60px;
                        height: 60px;
                    }

                    p {
                        padding-top: 10px;
                    }

                    &:hover {
                        p {
                            color: $mainColor;
                        }
                    }
                }
            }
        }
    }
}
</style>